/*
* Copyright 2017 Yonyou Auto Information Technology（Shanghai） Co., Ltd. All Rights Reserved.
*
* This software is published under the terms of the YONYOU Software
* License version 1.0, a copy of which has been included with this
* distribution in the LICENSE.txt file.
*
* @Project Name : marketing-service-dealer
*
* @File name : RoadSOSService.java
*
* @Author : Administrator
*
* @Date : 2017年12月5日
*
----------------------------------------------------------------------------------
*     Date       Who       Version     Comments
* 1. 2017年12月5日    Administrator    1.0
*
*
*
*
----------------------------------------------------------------------------------
*/
	
package com.yonyou.gmmc.service.dealer.service;


import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.apache.log4j.Logger;
import org.marketing.common.constants.ResultMsgConstants;
import org.marketing.common.exception.ServiceBizException;
import org.marketing.common.util.CheckUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.yonyou.gmmc.service.dealer.dao.TmUsersMapper;
import com.yonyou.gmmc.service.dealer.dao.TtSoshelpMapper;
import com.yonyou.gmmc.service.dealer.model.TmUsers;
import com.yonyou.gmmc.service.dealer.model.TtSoshelp;



/**
*
* @author Administrator
* 企业号道路救援API
* @date 2017年12月5日
*/

@Service
@Transactional
public class RoadSosService {

    private Logger logger = Logger.getLogger(RoadSosService.class);

    @Autowired
    private TmUsersMapper tmUsersMapper;
    
    @Autowired
    private TtSoshelpMapper ttSoshelpMapper;
    
    /**
     * @author QuanKK
     * @date 2018年1月19日 下午5:52:16
     * @param phone
     * @return
     */
    public List<TmUsers> queryTmUsersList(String phone){
		try{
        	List<TmUsers> tmUsersList = tmUsersMapper.queryTmUsersList(phone);
            return tmUsersList;
		}catch (Exception e){
	        logger.error(e.getMessage());
	        throw new ServiceBizException(e.getMessage(), e);
	    }
	}
    
	/**
	 * 救援列表
	 * @author QuanKK
	 * @date 2018年1月19日 下午5:52:20
	 * @param paramMap
	 * @return
	 */
	public PageInfo<TtSoshelp> querySosHelp(Map<String, Object> paramMap) throws Exception{
		try{
			//判断是否
	    	String phone = paramMap.get("phone").toString();
	    	List<TmUsers> tmUsersList = queryTmUsersList(phone);
	    	if(CheckUtil.NullOrEmpty(tmUsersList)){
	    		//用户不存在
	    		throw new ServiceBizException("用户不存在！");
	    	}else if(tmUsersList.size()>1){
	    		//用户存在多条数据
	    		throw new ServiceBizException("用户信息错误！");
	    	}
	    	String dealerCode = tmUsersList.get(0).getDealerCode();
	    	logger.info("售后顾问dealerCode="+dealerCode);
        	if(CheckUtil.NullOrEmpty(dealerCode)){
        		//经销商代码不存在
        		throw new ServiceBizException("用户信息错误！");
        	}
	    	paramMap.put("dealerCode", dealerCode);
	    	Page<TtSoshelp> ss = PageHelper.startPage(Integer.parseInt(paramMap.get("pageNum").toString()),
                    								 Integer.parseInt(paramMap.get("pageSize").toString()));
	    	logger.info("ss为————" + ss);
	    	List<TtSoshelp> sosHelpList = ttSoshelpMapper.querySosHelp(paramMap);
	    	PageInfo<TtSoshelp> page = new PageInfo<TtSoshelp>(sosHelpList);
	    	return page;
		}catch (Exception e){
            logger.error(e.getMessage());
            throw new ServiceBizException(e.getMessage(), e);
        }
	}

	/**
	 * 救援详情
	 * @author QuanKK
	 * @date 2018年1月26日 下午4:54:35
	 * @param soshelpId
	 * @param phone
	 * @return
	 */
	public TtSoshelp querySosDetail(Long soshelpId, String phone) {
		try{
			//判断是否
	    	List<TmUsers> tmUsersList = queryTmUsersList(phone);
	    	if(CheckUtil.NullOrEmpty(tmUsersList)){
	    		//用户不存在
	    		throw new ServiceBizException("用户不存在！");
	    	}else if(tmUsersList.size()>1){
	    		//用户存在多条数据
	    		throw new ServiceBizException("用户信息错误！");
	    	}
	    	String dealerCode = tmUsersList.get(0).getDealerCode();
	    	logger.info("售后顾问dealerCode="+dealerCode);
        	if(CheckUtil.NullOrEmpty(dealerCode)){
        		//经销商代码不存在
        		throw new ServiceBizException("用户信息错误！");
        	}
        	TtSoshelp TtSoshelp = ttSoshelpMapper.querySosDetail(soshelpId,dealerCode);
			return TtSoshelp;
		}catch (Exception e){
            logger.error(e.getMessage());
            throw new ServiceBizException(e.getMessage(), e);
        }
	}

	/**
	 * 立即救援
	 * @author QuanKK
	 * @date 2018年1月26日 下午5:11:08
	 * @param soshelpId
	 * @param phone
	 * @return
	 */
	public Map<String, Object> rescueSos(Long soshelpId, String phone) {
		Map<String, Object> result = new HashMap<String, Object>();
		try{
			//判断售后顾问是否存在
	    	logger.info("售后顾问phone="+phone);
	    	List<TmUsers> tmUsersList = queryTmUsersList(phone);
	    	if(CheckUtil.NullOrEmpty(tmUsersList)){
	    		//用户不存在
	    		throw new ServiceBizException("用户不存在！");
	    	}else if(tmUsersList.size()>1){
	    		//用户存在多条数据
	    		throw new ServiceBizException("用户信息错误！");
	    	}
	    	String dealerCode = tmUsersList.get(0).getDealerCode();
	    	logger.info("售后顾问dealerCode="+dealerCode);
        	if(CheckUtil.NullOrEmpty(dealerCode)){
        		//经销商代码不存在
        		throw new ServiceBizException("用户信息错误！");
        	}
        	TtSoshelp ttSoshelp = ttSoshelpMapper.querySosDetail(soshelpId,dealerCode);
        	if(!CheckUtil.NullOrEmpty(ttSoshelp)){
        		if(ttSoshelp.getStatus()==10111001){
        			TtSoshelp record = new TtSoshelp();
            		Integer userId = tmUsersList.get(0).getUserId();
            		record.setSoshelpId(soshelpId.longValue());
            		record.setStatus(10111002);
            		record.setSalesConsultantId(userId.longValue());
            		record.setUpdateDate(new Date());
            		record.setUpdateBy(userId.longValue());
            		int a = ttSoshelpMapper.updateByPrimaryKeySelective(record);
            		result.put(ResultMsgConstants.RESULT_FLAG, ResultMsgConstants.RESULT_FLAG_SUCCESS);
        			result.put(ResultMsgConstants.RESULT_MESSAGE, "立即救援成功！");
            	}else{
            		result.put(ResultMsgConstants.RESULT_FLAG, ResultMsgConstants.RESULT_FLAG_ERROR);
        			result.put(ResultMsgConstants.ERR_MSG, "状态错误，不能救援！");
            	}
        	}else{
        		result.put(ResultMsgConstants.RESULT_FLAG, ResultMsgConstants.RESULT_FLAG_ERROR);
    			result.put(ResultMsgConstants.ERR_MSG, "救援订单不存在！");
        	}
			return result;
		}catch (Exception e){
            logger.error(e.getMessage());
            throw new ServiceBizException(e.getMessage(), e);
        }
	}

	/**
	 * 取消救援
	 * @author QuanKK
	 * @date 2018年1月26日 下午5:29:28
	 * @param soshelpId
	 * @param phone
	 * @return
	 */
	public Map<String, Object> cancelSos(Long soshelpId, String phone) {
		Map<String, Object> result = new HashMap<String, Object>();
		try{
			//判断售后顾问是否存在
	    	logger.info("售后顾问phone="+phone);
	    	List<TmUsers> tmUsersList = queryTmUsersList(phone);
	    	if(CheckUtil.NullOrEmpty(tmUsersList)){
	    		//用户不存在
	    		throw new ServiceBizException("用户不存在！");
	    	}else if(tmUsersList.size()>1){
	    		//用户存在多条数据
	    		throw new ServiceBizException("用户信息错误！");
	    	}
	    	String dealerCode = tmUsersList.get(0).getDealerCode();
	    	logger.info("售后顾问dealerCode="+dealerCode);
        	if(CheckUtil.NullOrEmpty(dealerCode)){
        		//经销商代码不存在
        		throw new ServiceBizException("用户信息错误！");
        	}
        	TtSoshelp ttSoshelp = ttSoshelpMapper.querySosDetail(soshelpId,dealerCode);
        	if(!CheckUtil.NullOrEmpty(ttSoshelp)){
        		Long userId = tmUsersList.get(0).getUserId().longValue();
        		Long salesConsultantId = ttSoshelp.getSalesConsultantId();
        		if(salesConsultantId.equals(userId)){
        			if(ttSoshelp.getStatus()==10111002){
            			TtSoshelp record = new TtSoshelp();
                		record.setSoshelpId(soshelpId);
                		record.setStatus(10111004);
                		record.setUpdateDate(new Date());
                		record.setUpdateBy(userId.longValue());
                		int a = ttSoshelpMapper.updateByPrimaryKeySelective(record);
                		result.put(ResultMsgConstants.RESULT_FLAG, ResultMsgConstants.RESULT_FLAG_SUCCESS);
            			result.put(ResultMsgConstants.RESULT_MESSAGE, "取消救援成功！");
                	}else{
                		result.put(ResultMsgConstants.RESULT_FLAG, ResultMsgConstants.RESULT_FLAG_ERROR);
            			result.put(ResultMsgConstants.ERR_MSG, "状态错误，不能取消救援！");
                	}
        		}else{
        			result.put(ResultMsgConstants.RESULT_FLAG, ResultMsgConstants.RESULT_FLAG_ERROR);
        			result.put(ResultMsgConstants.ERR_MSG, "顾问信息错误，不能取消救援！");
        		}
        	}else{
        		result.put(ResultMsgConstants.RESULT_FLAG, ResultMsgConstants.RESULT_FLAG_ERROR);
    			result.put(ResultMsgConstants.ERR_MSG, "救援订单不存在！");
        	}
			return result;
		}catch (Exception e){
            logger.error(e.getMessage());
            throw new ServiceBizException(e.getMessage(), e);
        }
	}

	/**
	 * 完成救援
	 * @author QuanKK
	 * @date 2018年1月26日 下午5:35:30
	 * @param soshelpId
	 * @param phone
	 * @return
	 */
	public Map<String, Object> completeSos(Long soshelpId, String phone) {
		Map<String, Object> result = new HashMap<String, Object>();
		try{
			//判断售后顾问是否存在
	    	logger.info("售后顾问phone="+phone);
	    	List<TmUsers> tmUsersList = queryTmUsersList(phone);
	    	if(CheckUtil.NullOrEmpty(tmUsersList)){
	    		//用户不存在
	    		throw new ServiceBizException("用户不存在！");
	    	}else if(tmUsersList.size()>1){
	    		//用户存在多条数据
	    		throw new ServiceBizException("用户信息错误！");
	    	}
	    	String dealerCode = tmUsersList.get(0).getDealerCode();
	    	logger.info("售后顾问dealerCode="+dealerCode);
        	if(CheckUtil.NullOrEmpty(dealerCode)){
        		//经销商代码不存在
        		throw new ServiceBizException("用户信息错误！");
        	}
        	TtSoshelp ttSoshelp = ttSoshelpMapper.querySosDetail(soshelpId,dealerCode);
        	if(!CheckUtil.NullOrEmpty(ttSoshelp)){
        		Long userId = tmUsersList.get(0).getUserId().longValue();
        		Long salesConsultantId = ttSoshelp.getSalesConsultantId();
        		if(salesConsultantId.equals(userId)){
        			if(ttSoshelp.getStatus()==10111002){
            			TtSoshelp record = new TtSoshelp();
                		record.setSoshelpId(soshelpId);
                		record.setStatus(10111003);
                		record.setUpdateDate(new Date());
                		record.setUpdateBy(userId.longValue());
                		int a = ttSoshelpMapper.updateByPrimaryKeySelective(record);
                		result.put(ResultMsgConstants.RESULT_FLAG, ResultMsgConstants.RESULT_FLAG_SUCCESS);
            			result.put(ResultMsgConstants.RESULT_MESSAGE, "完成救援成功！");
                	}else{
                		result.put(ResultMsgConstants.RESULT_FLAG, ResultMsgConstants.RESULT_FLAG_ERROR);
            			result.put(ResultMsgConstants.ERR_MSG, "状态错误，不能完成救援！");
                	}
        		}else{
        			result.put(ResultMsgConstants.RESULT_FLAG, ResultMsgConstants.RESULT_FLAG_ERROR);
        			result.put(ResultMsgConstants.ERR_MSG, "顾问信息错误，不能完成救援！");
        		}
        	}else{
        		result.put(ResultMsgConstants.RESULT_FLAG, ResultMsgConstants.RESULT_FLAG_ERROR);
    			result.put(ResultMsgConstants.ERR_MSG, "救援订单不存在！");
        	}
			return result;
		}catch (Exception e){
            logger.error(e.getMessage());
            throw new ServiceBizException(e.getMessage(), e);
        }
	}
    
    
	
	
	
}
